<style lang="less">
  @import '../styles/common.less';
  @import '../tables/components/table.less';
</style>

<template>
  <div>
    <Row :gutter="10">
      <Card>
        <div class="table-head">
          <Button type="primary" class="Button" @click="ExportData()">导出数据</Button>
          <Button type="primary" class="Button" @click="remove()">删除</Button>
          <Button type="primary" class="Button" @click="add()">添加</Button>
        </div>
        <Row class="margin-top-10">
          <Table stripe border :columns="columns" :data="data" @on-row-click="onRowData"
                 @on-selection-change="selectionChange"></Table>
        </Row>
        <div style="margin: 10px;overflow: hidden;height: 36px;">
          <div style="float: right;">
            <Page :total="total" :current="page" @on-change="changePage" show-elevator></Page>
          </div>
        </div>
      </Card>
      <Modal :width="600" title="详情"
             v-model="showCurrentTableData"
             ok-text="确定"
             cancel-text="取消"
             @on-ok=""
             @on-cancel=""
             class-name="vertical-center-modal"
             class="Modal-Detail-div">
        <Form label-position="top">
          <FormItem label="名称">
            <div class="dataDetail-div" v-text="dataDetail.name"></div>
          </FormItem>
          <FormItem label="最大容纳人数（人）" v-if="dataDetail.maximum">
            <div class="dataDetail-div" v-text="dataDetail.maximum"></div>
          </FormItem>
          <FormItem label="座椅数量（个）" v-if="dataDetail.seat">
            <div class="dataDetail-div" v-text="dataDetail.seat"></div>
          </FormItem>
          <FormItem label="是否支持多媒体">
            <div class="dataDetail-div" v-text="dataDetail.is_projector==true ? '是' : '否'"></div>
          </FormItem>
          <FormItem label="特殊说明" v-if="dataDetail.description">
            <div class="dataDetail-div" v-text="dataDetail.description"></div>
          </FormItem>

        </Form>
      </Modal>
      <!--编辑-->
      <Modal v-model="addCurrentTableData" :width="600" :mask-closable="false" :closable="false"
             class-name="vertical-center-modal" class="Modal-Detail-div">
        <h3 slot="header" style="color:#2D8CF0">编辑</h3>
        <Form label-position="top">
          <FormItem label="名称">
            <Input v-model="addData.name" placeholder="名称"></Input>
          </FormItem>
          <FormItem label="最大容纳人数（人）">
            <InputNumber :max="1000" :min="1" v-model="addData.maximum" class="InputNumber"></InputNumber>
          </FormItem>
          <FormItem label="座椅数量（个）">
            <InputNumber :max="1000" :min="1" v-model="addData.seat" class="InputNumber"></InputNumber>
          </FormItem>
          <FormItem label="是否支持多媒体">
            <i-switch v-model="addData.is_projector" size="large">
              <span slot="open">是</span>
              <span slot="close">否</span>
            </i-switch>
          </FormItem>
          <FormItem label="特殊说明">
            <Input v-model="addData.description" type="textarea" class="textarea_resize"
                   :autosize="{minRows: 3}" placeholder="特殊说明"></Input>
          </FormItem>
        </Form>
        <div slot="footer">
          <Button type="text" @click="hideAddModal">取消</Button>
          <Button type="primary" :loading="saveLoading" @click="save()">确定</Button>
        </div>
      </Modal>
    </Row>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import Util from '../../libs/util';
  import Utils from '../../libs/mod.utils';
  import String from '../../libs/mod.string';
  import Modal from '../../libs/mod.Modal';
  export default {
    components: {},
    data () {
      return {
        total: 0,
        page: 1,
        size: 10,
        id: '',
        searchConName: '',
        searchConTel: '',
        showCurrentTableData: false,
        loading: true,
        showCurrentColumns: [],
        editInlineAndCellData: [],
        columns: [
          {
            type: 'selection',
            width: 60,
            align: 'center'
          },
          {
            key: 'name',
            title: '名称',
          },
          {
            key: 'maximum',
            title: '最大容纳人数（人）',
            width: 180,
          },
          {
            key: 'seat',
            title: '座椅数量（个）',
            width: 180,
          },
          {
            key: 'is_projector',
            title: '是否支持多媒体',
            width: 180,
            render: (h, params) => {
              return h('div', [
                h('span', params.row.is_projector == true ? '是' : '否')
              ]);
            }
          },
          {
            title: 'Action',
            key: 'action',
            width: 150,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('Button', {
                  props: {
                    type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: e => {
                      e.stopPropagation();
                      var data = params.row;
                      this.update(data, params.index);
                    }
                  }
                }, '修改')
              ]);
            }
          }
        ],
        data: [],
        dataDetail: {},
        initTable: [],
        addCurrentTableData: false,
        saveLoading: false,
        ajaxType: 'add',
        addData: {
          name: '',
          maximum: 1,
          seat: 1,
          is_projector: false,
          description: '',
        },
        selectionArr: [],
      };
    },
    mounted () {
      this.getData();
    },
    methods: {
      getData () {
        var _this = this;
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          page: _this.page,
          size: _this.size,
        };
        var params = {
          url: '/meeting-room/list',
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var _data = res.data || [];
            _this.total = res.rows;
            _this.data = _data;
          } else {
            _this.$Message.error('查询信息失败!');
          }
        }
      },
      handleSearch () {
        var _this = this;
        _this.page = 1;
        var searchConName = _this.searchConName;
      },
      changePage (page) {
        var _this = this;
        console.log('page', page);
        _this.page = page;
        _this.getData();
      },
      onRowData: function (data, index) {
        var _this = this;
        _this.show(index, data);
      },
      show: function (index, data) {
        var _this = this;
        var id = data.id;
        _this.id = id;

        _this.dataDetail = data;
        _this.showCurrentTableData = true;
      },
      asyncOK: function () {
        var _this = this;
        _this.showCurrentTableData = false;
      },
      // 列表多选 选中数据
      selectionChange: function (selection) {
        var _this = this;
        _this.selectionArr = selection;
      },
      // 隐藏编辑页面
      hideAddModal: function () {
        var _this = this;
        _this.addCurrentTableData = false;
      },
      // 显示添加界面
      add: function () {
        var _this = this;
        _this.addData.name = '';
        _this.addData.maximum = 1;
        _this.addData.is_projector = false;
        _this.addData.seat = 1;
        _this.addData.description = '';
        _this.ajaxType = 'add';
        _this.addCurrentTableData = true;
      },
      // 保存数据
      save: function () {
        var _this = this;
        var name = _this.addData.name;
        var maximum = _this.addData.maximum;
        var seat = _this.addData.seat;
        var is_projector = _this.addData.is_projector;
        var description = _this.addData.description;
        if (!(name && name != '')) {
          _this.$Message.error('请填写名称!');
          return false;
        }
        var data = {
          id: _this.id,
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          name: name,
          maximum: maximum,
          is_projector: is_projector,
          seat: seat,
          description: description
        };
        var url = '/meeting-room/add';
        if (_this.ajaxType == 'update') {
          url = '/meeting-room/update';
        }
        var params = {
          url: url,
          type: Util.method.get,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        _this.saveLoading = true;
        function _successFun(res) {
          _this.saveLoading = false;
          if (res.result == 0) {
            _this.page = 1;
            _this.getData();
            _this.addCurrentTableData = false;
            _this.addData.name = '';
            _this.addData.maximum = 1;
            _this.addData.is_projector = false;
            _this.addData.seat = 1;
            _this.addData.description = '';
          } else {
            _this.$Message.error('保存信息失败!');
          }
        }

      },
      // 显示修改界面
      update: function (data, index) {
        var _this = this;
        _this.ajaxType = 'update';
        var id = data.id;
        _this.id = id;
        _this.addData.name = data.name;
        _this.addData.maximum = data.maximum;
        _this.addData.is_projector = data.is_projector;
        _this.addData.seat = data.seat;
        _this.addData.description = data.description;

        _this.addCurrentTableData = true;
      },
      // 删除
      remove: function () {
        var _this = this;
        var selectionArr = _this.selectionArr;
        if (selectionArr.length == 0) {
          _this.$Message.error('请填选择要删除的数据!');
          return false;
        }
        var selectionIds = [];
        for (var k in selectionArr) {
          if (!selectionArr[k]) {
            continue;
          }
          var id = selectionArr[k].id;
          selectionIds.push(id);
        }
        Modal.confirm(_this, {content: '确定删除选中的信息？'}, function () {
          var data = {
            id: selectionIds,
          };
          var params = {
            url: '/meeting-room/deleteMultiple',
            type: Util.method.get,
            data: data,
            success: _successFun
          };
          Util.ajax(_this, params);
          function _successFun(res) {
            if (res.result == 0) {
              _this.page = 1;
              _this.getData();
            } else {
              _this.$Message.error('删除信息失败!');
            }
          }
        }, function () {
        });
      },
      // 导出
      ExportData: function () {
        var _this = this;
        var fields = [
          {
            field: 'id',
            title: '#',
            type: 'int',
            order: 1,
          },
          {
            field: 'name',
            title: '名称',
            type: 'varchar',
            order: 2,
          },
          {
            field: 'maximum',
            title: '最大容纳人数',
            type: 'int',
            order: 3,
          },
          {
            field: 'seat',
            title: '座椅数量',
            type: 'int',
            order: 4,
          },
          {
            field: 'is_projector',
            title: '是否支持多媒体',
            type: 'varchar',
            order: 5,
          },
          {
            field: 'description',
            title: '特殊说明',
            type: 'varchar',
            order: 6,
          },
        ];
        var fieldsStr = Util.toStr(fields);
        var data = {
          uid: Util.toJson(localStorage.getItem('userInfo')).id,
          type: 'meetingRoom',
          fields: fieldsStr,
        };
        var params = {
          url: '/file/db2excle',
          type: Util.method.post,
          data: data,
          success: _successFun
        };
        Util.ajax(_this, params);
        function _successFun(res) {
          if (res.result == 0) {
            var url = res.data;
            var date = Utils.dateToDateString(new Date());
            Utils.DownloadFile(url, '导出会议室信息_' + date + '.xlsx');
          } else {
            _this.$Message.error('导出信息失败!');
          }
        }
      },
    }
  };
</script>
